home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-05-29 | 31.2 KB | 950 lines | [TEXT/MPS ] |
- unit UDialogs;
- (* Written by Richard Clark (AppleLink, Delphi, GEnie, MCI, MouseHole: RDCLARK *)
- (* Internet: rdclark@apple.com or rdclark@applelink.apple.com) *)
- (* Copyright (c) 1989 by Apple Computer, Inc. All Rights Reserved *)
- {$S UDialogs}
-
- interface
-
- uses
- Types, Memory, QuickDraw, Menus, Dialogs, Packages, Errors, OSUtils, ToolUtils,
- UGlobals, UHeapHandler;
-
- const
- (* Item numbers in the dialog box *)
- dmOldHeap = 1; (* The "Old Heap" display in the Memory dialog *)
- dmNewHeap = 2; (* The "New Heap" display in the Memory dialog *)
- diNewHandle = 3; (* The "NewHandle" button *)
- diHLock = 4; (* The "HLock" button *)
- diHUnlock = 5; (* The "HUnlock" button *)
- diMoveHHi = 6; (* The "MoveHHi" button *)
- diDisposeHand = 7; (* The "DisposHandle" button *)
- diNewPtr = 8; (* The "NewPtr" button *)
- diDisposPtr = 9; (* The "DisposPtr" button *)
- diHandSize = 10; (* The "New Handle Size" edit text item *)
- diPtrSize = 11; (* The "New Pointer Size" edit text item *)
- (* Item numbers for the extended dialog *)
- (* Item 17 is the "Memory Size" static text label *)
- diMemSizeItem = 18; (* The static text item representing th available memory *)
- (* Item 19 is the "Largest Free" static text label *)
- diContigItem = 20; (* The static text item representing the largest contiguous block after a purge *)
- (* Item 21 is the "Result" static text item *)
- diResultItem = 22; (* The result (code or number) of the last operation *)
- diHPurge = 23; (* The "HPurge" button (advanced dialog) *)
- diHNoPurge = 24; (* The "HNoPurge" button (advanced dialog) *)
- diMaxMem = 25; (* The "MaxMem" button (advanced dialog) *)
- diCompactMem = 26; (* The "CompactMem" button (advanced dialog) *)
- diPurgeMem = 27; (* The "PurgeMem" button (advanced dialog) *)
- diCMSize = 28; (* The "CompactMem" paramater edit text item *)
- diPMSize = 29; (* The "PurgeMem" paramater edit text item *)
-
- lastDialogItem = 29; (* How many items are in my dialog *)
-
- procedure InitMyDialogs;
-
- procedure OpenMemoryDialog;
- procedure InitMemoryDialog;
- procedure InvalItem (whichItem: integer);
- procedure CloseMemoryDialog;
-
- function HandleDialogEvents (theEvent: EventRecord): Boolean;
-
- implementation
-
- (* Private procedures *)
-
- procedure DrawItem (whichWindow: WindowPtr; itemNum: integer);
- (* Draw the specified "heap" user items *)
- var
- itemType: integer;
- itemHand: Handle;
- itemRect: Rect;
-
- begin
- SetPort(whichWindow);
- (* Get the boundaries of the given display *)
- GetDItem(DialogPtr(whichWindow), itemNum, itemType, itemHand, itemRect);
-
- case itemNum of
- dmOldHeap:
- DrawHeap(OldHeap, kDontShowSelection);
-
- dmNewHeap:
- DrawHeap(CurrHeap, kDoShowSelection);
- end;
- end; (* DrawHeap *)
-
- procedure EnableButton (whichItem: integer; enabled: Boolean);
- var
- itemType: integer;
- itemHand: Handle;
- itemRect: Rect;
-
- begin
- (* Mark this item as having been changed *)
- GetDItem(memoryDialog, whichItem, itemType, itemHand, itemRect);
- if enabled then
- HiliteControl(ControlHandle(itemHand), 0)
- else
- HiliteControl(ControlHandle(itemHand), 255);
- end; (* EnableButton *)
-
- procedure BuildNumericString (theNumber: LONGINT; var theString: Str255);
- var
- otherNumString, deltaString: Str255;
- delta: LONGINT;
-
- begin
- NumToString(theNumber, theString);
- Insert(' bytes (', theString, length(theString) + 1);
- NumToString((theNumber + 512) div 1024, otherNumString);
- Insert('K', otherNumString, length(otherNumString) + 1);
- if (theNumber mod 1024) > 512 then
- delta := (theNumber mod 1024) - 1024
- else
- delta := theNumber mod 1024;
- if delta <> 0 then
- begin
- NumToString(delta, deltaString);
- Insert(deltaString, otherNumString, length(otherNumString) + 1);
- end;
- Insert(')', otherNumString, length(otherNumString) + 1);
- Insert(otherNumString, theString, length(theString) + 1);
- end; (* BuildNumericString*)
-
-
- procedure ShowNumResult (theDialog: DialogPtr; result: LONGINT);
- var
- resultString: Str255;
- itemType: INTEGER;
- itemHand: handle;
- itemRect: Rect;
-
- begin
- BuildNumericString(result, resultString);
- GetDItem(theDialog, diResultItem, itemType, itemHand, itemRect);
- SetIText(itemHand, resultString);
- end; (* ShowNumResult *)
-
-
- procedure ShowMemError (theDialog: DialogPtr; result: INTEGER);
- var
- errorString: Str255;
- itemType: INTEGER;
- itemHand: Handle;
- itemRect: Rect;
-
- begin
- GetDItem(theDialog, diResultItem, itemType, itemHand, itemRect);
- case result of
- noErr:
- errorString := 'noErr';
-
- memFullErr:
- errorString := 'memFullErr';
-
- memPurErr:
- errorString := 'memPurErr';
-
- memWZErr:
- errorString := 'memWZErr';
- otherwise
- NumToString(result, errorString);
- end;
- SetIText(itemHand, errorString);
- end; (* ShowMemError *)
-
-
- procedure ShowFreeMem (theDialog: DialogPtr);
- var
- oldValue, newValue: Str255;
- memAvail, memContig: LONGINT;
- itemType: INTEGER;
- itemHand: Handle;
- itemRect: Rect;
-
- begin
- SetZone(MyDemoZone);
- if System.EnhancedROMs then
- PurgeSpace(memAvail, memContig)
- else
- memAvail := FreeMem; (* div 1024 *)
- SetZone(MyAppZone);
-
- BuildNumericString(memAvail, newValue);
- GetDItem(theDialog, diMemSizeItem, itemType, itemHand, itemRect);
- GetIText(itemHand, oldValue);
- if (oldValue <> newValue) then
- SetIText(itemHand, newValue);
-
- if System.EnhancedROMs then
- BuildNumericString(memContig, newValue)
- else
- newValue := 'Not available';
- GetDItem(theDialog, diContigItem, itemType, itemHand, itemRect);
- GetIText(itemHand, oldValue);
- if (oldValue <> newValue) then
- SetIText(itemHand, newValue);
-
- end; (* ShowFreeMem *)
-
-
-
- procedure InitMemoryDialog;
- var
- itemType: integer;
- itemHand: Handle;
- itemRect: Rect;
-
- begin
- (* Install the 2 heap displays as user items *)
- GetDItem(memoryDialog, dmOldHeap, itemType, itemHand, itemRect);
- SetDItem(memoryDialog, dmOldHeap, itemType, Handle(@DrawItem), itemRect);
- OldHeap.heapRect := itemRect;
- with itemRect do
- BytesPerPixel := (MyHeapSize * 1024) div (bottom - top);
- GetDItem(memoryDialog, dmNewHeap, itemType, itemHand, itemRect);
- SetDItem(memoryDialog, dmNewHeap, itemType, Handle(@DrawItem), itemRect);
- CurrHeap.heapRect := itemRect;
-
- (* Enable and disable the appropriate buttons *)
- EnableButton(diNewHandle, TRUE);
- EnableButton(diHLock, FALSE);
- EnableButton(diMoveHHi, FALSE);
- EnableButton(diHUnlock, FALSE);
- if application.UseExtendedDialog then
- begin
- EnableButton(diHPurge, FALSE);
- EnableButton(diHNoPurge, FALSE);
- end;
- EnableButton(diDisposeHand, FALSE);
- EnableButton(diNewPtr, TRUE);
- EnableButton(diDisposPtr, FALSE);
-
- SetWRefCon(memoryDialog, MemDialogRefCon);
- ShowMemError(memoryDialog, 0);
- ShowFreeMem(memoryDialog);
- end; (* InitMemoryDialog *)
-
- (* Public Procedures *)
-
- procedure SelectBlock (whichBlock: INTEGER; var whichHeap: HeapInfo; heapRect: Rect);
- (* Mark te specific block as "selected" (i.e. highlighted). 0 de-selects all blocks *)
- var
- oldSelection: INTEGER;
- isLocked, isPurgeable: Boolean;
-
- begin
- oldSelection := whichHeap.selectedBlock;
- whichHeap.selectedBlock := whichBlock;
- if (oldSelection <> 0) then
- DrawBlock(oldSelection, whichHeap, kDoShowSelection);
- if (whichBlock <> 0) then
- begin
- case CurrHeap.blocks[whichBlock].blkType of
- blkHandle:
- begin
- isLocked := CurrHeap.blocks[whichBlock].blkLocked;
- isPurgeable := CurrHeap.blocks[whichBlock].blkPurgeable;
- EnableButton(diHLock, not isLocked);
- EnableButton(diHUnlock, isLocked);
- EnableButton(diMoveHHi, not isLocked);
- if application.UseExtendedDialog then
- begin
- EnableButton(diHPurge, not isPurgeable);
- EnableButton(diHNoPurge, isPurgeable);
- end;
- EnableButton(diDisposeHand, TRUE);
- EnableButton(diDisposPtr, FALSE);
- end;
-
- blkPointer:
- begin
- EnableButton(diHLock, FALSE);
- EnableButton(diHUnlock, FALSE);
- EnableButton(diMoveHHi, FALSE);
- EnableButton(diDisposeHand, FALSE);
- EnableButton(diDisposPtr, TRUE);
- if application.UseExtendedDialog then
- begin
- EnableButton(diHPurge, FALSE);
- EnableButton(diHNoPurge, FALSE);
- end;
- end;
- end; (* case *)
- DrawBlock(whichBlock, whichHeap, kDoShowSelection);
- end
- else
- begin
- EnableButton(diHLock, FALSE);
- EnableButton(diHUnlock, FALSE);
- EnableButton(diMoveHHi, FALSE);
- EnableButton(diDisposeHand, FALSE);
- EnableButton(diDisposPtr, FALSE);
- if application.UseExtendedDialog then
- begin
- EnableButton(diHPurge, FALSE);
- EnableButton(diHNoPurge, FALSE);
- end;
- end;
- end; (* SelectBlock *)
-
-
- procedure InitMyDialogs;
- (* Initialize dialog-related global variables *)
- begin
- memoryDialog := nil;
- end; (* InitMyDialogs *)
-
-
- procedure OpenMemoryDialog;
- var
- itemType: INTEGER;
- itemHand: Handle;
- itemRect: Rect;
-
- begin
- if (memoryDialog = nil) then
- begin
- if application.UseExtendedDialog then
- memoryDialog := GetNewDialog(dExtendedMemory, nil, WindowPtr(-1))
- else
- memoryDialog := GetNewDialog(dShortMemory, nil, WindowPtr(-1));
- if memoryDialog <> nil then
- begin
- InitMemoryDialog;
- DisableItem(FileMenu, ifOpen);
- GetDItem(memoryDialog, dmNewHeap, itemType, itemHand, itemRect);
- SelectBlock(0, CurrHeap, itemRect); (* De-select any previously chosen block *)
- ShowWindow(memoryDialog);
- (* Changes added for version 1.3.4 *)
- EnableItem(WindowMenu, imSimpleDialog);
- EnableItem(WindowMenu, imComplexDialog);
- IF CurrHeap.BlocksUsed > 0 THEN
- EnableItem(SpecialMenu, isEraseHeap)
- ELSE
- DisableItem(SpecialMenu, isEraseHeap);
- DrawMenuBar;
- end;
- end;
- end; (* OpenMemoryDialog *)
-
-
- procedure InvalItem;
- (* Invalidate the specified dialog item, forcing it to be redrawn *)
- var
- itemType: integer;
- itemHand: Handle;
- itemRect: Rect;
- oldPort: GrafPtr;
-
- begin
- GetDItem(memoryDialog, whichItem, itemType, itemHand, itemRect);
- GetPort(oldPort);
- SetPort(memoryDialog);
- InvalRect(itemRect);
- SetPort(oldPort);
- end; (* InvalItem *)
-
-
- procedure CloseMemoryDialog;
- begin
- if memoryDialog <> nil then
- begin
- DisposDialog(memoryDialog);
- memoryDialog := nil;
- EnableItem(FileMenu, ifOpen);
- (* Changes added for version 1.3.4 *)
- DisableItem(WindowMenu, imSimpleDialog);
- DisableItem(WindowMenu, imComplexDialog);
- DisableItem(SpecialMenu, isEraseHeap);
- DrawMenuBar;
- END;
- end; (* CloseMemoryDialog *)
-
-
- function GetItemNumber (theDialog: DialogPtr; theItem: INTEGER): LongInt;
- var
- n: LONGINT;
- itemString: Str255;
- itemType: INTEGER;
- item: Handle;
- bounds: Rect;
- i: Integer;
-
- begin
- GetDItem(theDialog, theItem, itemType, item, bounds);
- GetIText(item, itemString);
- for i := 1 to LENGTH(itemString) do
- begin
- if (itemString[i] < '0') or (itemString[i] > '9') then
- begin
- itemString[i] := '0'; (* We could do something better here⦠*)
- end;
- end;
- StringToNum(itemString, n);
- GetItemNumber := n;
- end; {of GetItemNumber}
-
-
- procedure DoMouseInHeap (theEvent: EventRecord; theDialog: DialogPtr; theItem: INTEGER);
- (* Handle mouse downs in one of the "heap" user items *)
- var
- itemType: INTEGER;
- itemhand: Handle;
- itemRect: Rect;
- (* The following are used by the block search mechanism *)
- byteOffset: LONGINT;
- blkCount: INTEGER;
- found: Boolean;
-
- begin
- (* This is called if the user clicks in the Current heap display area *)
- (* It should set the number of the currently selected block *)
- GlobalToLocal(theEvent.where);
- GetDItem(theDialog, theItem, itemType, itemHand, itemRect);
- if PtInRect(theEvent.where, itemRect) then
- begin
- found := FALSE;
- byteOffset := BytesPerPixel * (itemRect.bottom - theEvent.where.v) + ORD(MyDemoZone) + HeapBias;
- with CurrHeap do
- for blkCount := 1 to MyArraySize do
- with blocks[blkCount] do
- if (blkType <> blkFree) then
- if (byteOffset >= blkStart) and (byteOffset <= (blkStart + blkSize)) then
- begin
- SelectBlock(blkCount, CurrHeap, itemRect);
- found := TRUE;
- end;
- if not found then
- SelectBlock(0, CurrHeap, itemRect);
- end;
- end; (* DoMouseInHeap *)
-
-
- function CheckAllocRequest (requested: integer; theHeap: HeapInfo; var freeBlock: integer): boolean;
- (* Verify that the number lies between 1 and the heap size (16). If so, retrn TRUE. If not, put *)
- (* up an alert notifying the user of the error. This also returns the number of the first free block *)
- (* in the current heap array. *)
- var
- blockCount: integer;
- alertItem: integer;
- nBlocks: Str255;
-
- begin
- blockCount := 1;
- freeBlock := 0;
- (* Look for a free block in the Current Heap array *)
- while (blockCount <= MyArraySize) and (freeBlock = 0) do
- if theHeap.blocks[blockCount].blkType = blkFree then
- freeBlock := blockCount
- else
- blockCount := blockCount + 1;
-
- (* Call NumToStr and SetIText to insert MyHeapSize into the dialog's text *)
- NumToString(MyHeapSize, nBlocks);
- ParamText(nBlocks, '', '', '');
- if (freeBlock = 0) then
- begin
- alertItem := StopAlert(aNoMoreBlocks, nil);
- CheckAllocRequest := FALSE;
- end
- else if (requested < 1) or (requested > MyHeapSize) then
- begin
- alertItem := StopAlert(aBadNumber, nil);
- CheckAllocRequest := FALSE;
- end
- else
- CheckAllocRequest := TRUE;
- end; (* CheckAllocRequest *)
-
-
- function CheckGeneralRequest (requested: INTEGER): Boolean;
- (* Make sure that the number is between 0 and the block size (16). Put up an alert if not *)
- var
- alertItem: INTEGER;
-
- begin
- if (requested < 1) or (requested > MyHeapSize) then
- begin
- alertItem := StopAlert(aBadNumber, nil);
- CheckGeneralRequest := FALSE;
- end
- else
- CheckGeneralRequest := TRUE;
- end; (* CheckGeneralRequest *)
-
-
- function CheckResult (result: Ptr): Boolean;
- (* Alert the user if the supplied pointer (or handle) is nil *)
- var
- oldZone: THz;
- alertItem: integer;
-
- begin
- if (result = nil) then
- begin
- oldZone := GetZone;
- SetZone(MyAppZone);
- alertItem := NoteAlert(aNoDemoMemory, nil);
- SetZone(oldZone);
- CheckResult := FALSE;
- end
- else
- CheckResult := TRUE;
- end; (* CheckResult *)
-
- procedure HandleDialogClicks (theEvent: EventRecord; theDialog: DialogPtr; itemHit: INTEGER);
- (* Process clicks in the Memory dialog*)
- var
- scratchHand: Handle;
- scratchPtr: Ptr;
- freeBlock: integer;
- numBytes: LongInt;
- alertItem: integer;
- itemType: INTEGER;
- itemHand: Handle;
- itemRect: Rect;
- grow, result, remainder: LONGINT;
-
- begin
- SetPort(theDialog);
- if (itemHit = dmOldHeap) then
- alertItem := NoteAlert(aNoOldHeap, nil)
- else if (itemHit = dmNewHeap) then
- DoMouseInHeap(theEvent, theDialog, itemHit)
- else if (itemHit >= diNewHandle) and (itemHit <= lastDialogItem) then
- case itemHit of
-
- diNewHandle: {The "NewHandle" button was hit }
- begin
- numBytes := GetItemNumber(theDialog, diHandSize);
- (* Look for a valid size and a free block *)
- if (CheckAllocRequest(numBytes, CurrHeap, freeBlock)) then
- with CurrHeap do
- begin
- GetDItem(theDialog, dmNewHeap, itemType, itemHand, itemRect);
- SelectBlock(0, CurrHeap, itemRect); (* De-select any previously chosen block *)
- CopyHeapInfo(CurrHeap, OldHeap); (* Move the heap's status to the "old heap" array *)
- UpdateHeapInfo(OldHeap, kClearDirtyFlags); (* Calculate the physical locations of these blocks in the display *)
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone); (* Switch to the mini-heap, and do the operation *)
- scratchHand := NewHandle(numBytes * 1024 - BlockHeaderSize);
- result := MemError; (* get a result code *)
- SetZone(MyAppZone);
- if (CheckResult(Ptr(scratchHand))) then (* See if the allocation happened fine. If not,warn the user *)
- begin
- numBlocks := numBlocks + 1; (* Update the "currnet blocks" count *)
- blocksUsed := blocksUsed + 1; (* Update the block serial number counter *)
- with blocks[freeBlock] do
- begin (* Initialize this entry in the array *)
- blkStart := ORD(scratchHand^);
- blkSource := Ptr(scratchHand);
- blkType := blkHandle;
- blkSize := numBytes * 1024;
- blkSequence := numBlocks;
- HUnlock(scratchHand);
- HNoPurge(scratchHand);
- blkLocked := FALSE;
- blkPurgeable := FALSE;
- blkDirty := TRUE;
- end;
- end;
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- EnableButton(diNewHandle, TRUE); (* was "(blocksUsed < MyHeapSize) and (CurrHeap.MaxBlocks > 0)" *)
- EnableButton(diNewPtr, TRUE); (* was "(blocksUsed < MyHeapSize) and (CurrHeap.MaxBlocks > 0)" *)
- InvalItem(dmNewHeap); (* Redraw both heap displays *)
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result); (* Show the result of the last operation *)
- ShowFreeMem(theDialog); (* Show how much memory is left *)
- end;
- end;
-
- diNewPtr:
- begin
- numBytes := GetItemNumber(theDialog, diPtrSize); { diPtrSize is the NewHdl text box}
- if (CheckAllocRequest(numBytes, CurrHeap, freeBlock)) then
- with CurrHeap do
- begin
- GetDItem(theDialog, dmNewHeap, itemType, itemHand, itemRect);
- SelectBlock(0, CurrHeap, itemRect); (* De-select any previously chosen block *)
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone);
- scratchPtr := NewPtr(numBytes * 1024 - BlockHeaderSize);
- result := MemError;
- SetZone(MyAppZone);
- if (CheckResult(scratchPtr)) then
- begin
- numBlocks := numBlocks + 1;
- blocksUsed := blocksUsed + 1;
- with blocks[freeBlock] do
- begin
- blkStart := ORD(scratchPtr);
- blkOldStart := ORD(scratchPtr);
- blkSource := Ptr(scratchPtr);
- blkType := blkPointer;
- blkSize := numBytes * 1024;
- blkSequence := numBlocks;
- blkLocked := FALSE; (* Actually, this doesn't apply to this kind of block *)
- blkPurgeable := FALSE;
- blkDirty := TRUE;
- end;
- end;
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- EnableButton(diNewHandle, TRUE); (* was "(blocksUsed < MyHeapSize) and (CurrHeap.MaxBlocks > 0)" *)
- EnableButton(diNewPtr, TRUE); (* was "(blocksUsed < MyHeapSize) and (CurrHeap.MaxBlocks > 0)" *)
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end;
- end;
-
- diDisposeHand: {DisposHandle button}
- with CurrHeap do
- if selectedBlock <> 0 then
- begin
- with blocks[selectedBlock] do
- if (blkType <> blkHandle) then
- SysBeep(5)
- else
- begin
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- blocksUsed := blocksUsed - 1;
- blkType := blkFree;
- SetZone(MyDemoZone);
- DisposHandle(Handle(blkSource));
- result := MemError;
- SetZone(MyAppZone);
- blkSource := nil;
- blkStart := 0;
- blkDirty := TRUE;
- EnableButton(diNewHandle, TRUE);
- EnableButton(diNewPtr, TRUE);
- end;
- GetDItem(theDialog, dmNewHeap, itemType, itemHand, itemRect);
- SelectBlock(0, CurrHeap, itemRect); (* De-select any previously chosen block *)
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end
- else
- SysBeep(5);
-
- diDisposPtr:{DisposPtr button}
- with CurrHeap do
- if selectedBlock <> 0 then
- begin
- with blocks[selectedBlock] do
- if (blkType <> blkPointer) then
- SysBeep(5)
- else
- begin
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- blkType := blkFree;
- blocksUsed := blocksUsed - 1;
- SetZone(MyDemoZone);
- DisposPtr(blkSource);
- result := MemError;
- SetZone(MyAppZone);
- blkSource := nil;
- blkStart := 0;
- (* We won't change the size or start figures just yet *)
- blkDirty := TRUE;
- EnableButton(diNewHandle, TRUE);
- EnableButton(diNewPtr, TRUE);
- end;
- GetDItem(theDialog, dmNewHeap, itemType, itemHand, itemRect);
- SelectBlock(0, CurrHeap, itemRect); (* De-select any previously chosen block *)
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end
- else
- SysBeep(5);
-
- diHLock:
- with CurrHeap do
- if selectedBlock <> 0 then
- begin
- with blocks[selectedBlock] do
- if (blkType <> blkHandle) then
- SysBeep(5)
- else
- begin
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone);
- HLock(Handle(blkSource));
- result := MemError;
- SetZone(MyAppZone);
- blkLocked := TRUE;
- blkDirty := TRUE;
- EnableButton(diHLock, FALSE);
- EnableButton(diHUnlock, TRUE);
- EnableButton(diMoveHHi, FALSE);
- end;
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end
- else
- SysBeep(5);
-
- diHUnlock:
- with CurrHeap do
- if selectedBlock <> 0 then
- begin
- with blocks[selectedBlock] do
- if (blkType <> blkHandle) then
- SysBeep(5)
- else
- begin
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone);
- HUnlock(Handle(blkSource));
- result := MemError;
- SetZone(MyAppZone);
- blkLocked := FALSE;
- EnableButton(diHLock, TRUE);
- EnableButton(diHUnlock, FALSE);
- EnableButton(diMoveHHi, TRUE);
- blkDirty := TRUE;
- end;
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end
- else
- SysBeep(5);
-
- diMoveHHi:
- with CurrHeap do
- if selectedBlock <> 0 then
- begin
- with blocks[selectedBlock] do
- if (blkType <> blkHandle) then
- SysBeep(5)
- else
- begin
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- blkStart := ORD(blkSource^);
- SetZone(MyDemoZone);
- MoveHHi(Handle(blkSource));
- result := MemError;
- SetZone(MyAppZone);
- blkDirty := TRUE;
- end;
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end
- else
- SysBeep(5);
-
- diHPurge:
- with CurrHeap do
- if selectedBlock <> 0 then
- begin
- with blocks[selectedBlock] do
- if (blkType <> blkHandle) then
- SysBeep(5)
- else
- begin
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone);
- HPurge(Handle(blkSource));
- result := MemError;
- remainder := FreeMem;
- SetZone(MyAppZone);
- blkPurgeable := TRUE;
- EnableButton(diHPurge, FALSE);
- EnableButton(diHNoPurge, TRUE);
- EnableButton(diNewHandle, TRUE); (* remainder > 0 *)
- EnableButton(diNewPtr, TRUE); (* remainder > 0 *)
- blkDirty := TRUE;
- end;
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end
- else
- SysBeep(5);
-
- diHNoPurge:
- with CurrHeap do
- if selectedBlock <> 0 then
- begin
- with blocks[selectedBlock] do
- if (blkType <> blkHandle) then
- SysBeep(5)
- else
- begin
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone);
- HNoPurge(Handle(blkSource));
- result := MemError;
- SetZone(MyAppZone);
- blkPurgeable := FALSE;
- EnableButton(diHPurge, TRUE);
- EnableButton(diHNoPurge, FALSE);
- blkDirty := TRUE;
- end;
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end
- else
- SysBeep(5);
-
- diMaxMem:
- (* Copy the info, Call MaxMem, Update the heap, Inval the heaps *)
- with CurrHeap do
- begin
- GetDItem(theDialog, dmNewHeap, itemType, itemHand, itemRect);
- SelectBlock(0, CurrHeap, itemRect); (* De-select any previously chosen block *)
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone);
- grow := 0;
- result := MaxMem(grow);
- SetZone(MyAppZone);
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowNumResult(theDialog, result);
- ShowFreeMem(theDialog);
- end;
-
- diCompactMem:
- begin
- (* Get the CM Size, Check it, Copy the info, Call CompactMem, Update the heap, Inval the heaps *)
- numBytes := GetItemNumber(theDialog, diCMSize);
- if (CheckGeneralRequest(numBytes)) then
- with CurrHeap do
- begin
- GetDItem(theDialog, dmNewHeap, itemType, itemHand, itemRect);
- SelectBlock(0, CurrHeap, itemRect); (* De-select any previously chosen block *)
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone);
- result := CompactMem(numBytes * 1024); (* - BlockHeaderSize *)
- SetZone(MyAppZone);
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowFreeMem(theDialog);
- ShowNumResult(theDialog, result);
- end;
- end;
-
- diPurgeMem:
- begin
- (* Get the PM Size, Check it, Copy the info, Call PurgeMem, Update the heap, Inval the heaps *)
- numBytes := GetItemNumber(theDialog, diPMSize);
- if (CheckGeneralRequest(numBytes)) then
- with CurrHeap do
- begin
- GetDItem(theDialog, dmNewHeap, itemType, itemHand, itemRect);
- SelectBlock(0, CurrHeap, itemRect); (* De-select any previously chosen block *)
- CopyHeapInfo(CurrHeap, OldHeap);
- UpdateHeapInfo(OldHeap, kClearDirtyFlags);
- UpdateHeapInfo(CurrHeap, kClearDirtyFlags); (* clean up the "dirty" flags in the current heap now *)
- SetZone(MyDemoZone);
- PurgeMem(numBytes * 1024 - BlockHeaderSize);
- result := MemError;
- SetZone(MyAppZone);
- UpdateHeapInfo(CurrHeap, kLeaveDirtyFlags);
- InvalItem(dmNewHeap);
- InvalItem(dmOldHeap);
- ShowMemError(theDialog, result);
- ShowFreeMem(theDialog);
- end;
- end;
- end; {of case}
- (* Special menu enable logic added to version 1.3.4 *)
- IF CurrHeap.BlocksUsed > 0 THEN
- EnableItem(SpecialMenu, isEraseHeap)
- ELSE
- DisableItem(SpecialMenu, isEraseHeap);
- (* If the menu changed state, then re-draw the menu bar *)
- IF ((CurrHeap.blocksUsed = 0) AND (oldHeap.blocksUsed > 0)) OR
- ((CurrHeap.blocksUsed > 0) AND (oldHeap.blocksUsed = 0)) THEN
- DrawMenuBar;
- end; (* handleDialogClicks *)
-
-
- function HandleDialogEvents;
- const
- bs = 8;
- tab = 9;
- left = 28;
- right = 29;
-
- var
- whichDialog: DialogPtr;
- eventHandled: Boolean;
- itemHit: INTEGER;
- ch: char;
-
- begin
- eventHandled := false;
- if IsDialogEvent(theEvent) then
- begin
- (* Pass all dialog events to DialogSelect(), except for command-keys (for menus) *)
- if (theEvent.what = KeyDown) then
- begin
- if (BitAnd(theEvent.modifiers, cmdKey) = 0) then (* We have a dialog key, so filter out un-wanted characters *)
- begin
- ch := CHR(BitAnd(theEvent.message, charCodeMask));
- if (ch in ['0'..'9', CHR(bs), CHR(tab), CHR(left), CHR(right)]) then
- (* If it's a number, <Del>, or the right and left arrows, then process it *)
- eventHandled := DialogSelect(theEvent, whichDialog, itemHit)
- else
- SysBeep(5); (* Otherwise, complain *)
- end;
- end
- else (* Otherwise, it's not a key-down event *)
- begin
- if DialogSelect(theEvent, whichDialog, itemHit) then
- begin
- eventHandled := true;
- handleDialogClicks(theEvent, whichDialog, itemHit);
- end; (* case *)
- end; {of if DialogSelect}
- end;
- HandleDialogEvents := eventHandled;
- end; (* HandleDialogEvents *)
-
- end.